/*====Call On Page Load=====*/  

  

function doSave()
{
	

	  $.post("SaveData.php", { PageName: $("#txtPageName").val(),Description:$("#txtDescription").val() },
	  function(data){			   
	    if(data == "1")
	    {
		   alert("Page Saved Successfully");
	    }
	    else
	    {
		     alert("Error Occured");
	    }
	  }, "text");
}
function doCancel()
{
	if(confirm("You are about to cancel this page. This will delete all the page content. Are you sure you want to do this ?"))
	{
		window.location.reload(-1);
	}
	else
	{
		return false;
	}
}
function pageDataValidation()
{
	var pageName = $("#txtPageName").val();
	if(pageName.length==0)
	{
		alert("You must give a proper page name before creating this. Thank you.");
		return false;
	}
	else
	{
		var isPageExists = checkIfPageNameDuplicate(pageName);
		if(isPageExists=="1")
		{
			if(confirm("A page with the same name already exists. Are you really want to overwrite it ?"))
			{
				doSave();
			}
			else
			{
				return false;
			}
		}
		else
		{
			doSave();
		}
	}
}

function checkIfPageNameDuplicate(pagename)
{
	var message = "";
	var pagename = pagename;
	$.post("check-pagename-availability.php",{pagename:pagename},function(data){
		if(data=="1")
		{//duplicate
			message="1";
		}
		else
		{
			message="";
		}
	},"text");
	
	return message;
}
/*----------------------------------------------------------------------------------------*/


function deActivatePage(id)
{
	var deactivateId = id;
	if(deactivateId.length!=0)
	{
		if(confirm("Are you sure you want to deactivate this page ?"))
		{
			$.post("vehicle-actions.php",{action:"deactivate",selectedId:deletedId},function(data)
				{
					if(data==1)
					{
						alert("The Page was successfully deactivated. Thank You");
						window.location.reload();
					}
					else
					{
						alert("Problem Occured while deactivating this Page. Please try again later.");
						return false;
					}
				},"text");
		}
		else
		{
			return false;
		}
	}
}

function editPage(id)
{
	var editId = id;
	if(editId.length!=0)
	{
		if(confirm("Are you sure you want to edit this page ?"))
		{
			window.open("edit-page.php?editId="+editId,"_self");
		}
	}
}

function updatePageData()
{
	var hdnUpdatePageId = document.getElementById('hdnUpdatePageId').value;
	var txtDescription = document.form["frmEditPage"];
	if(hdnUpdatePageId.length!=0)
	{
		if(txtDescription.length!=0)
		{
			$.post("vehicle-actions.php",{action:"update",selectedId:hdnUpdatePageId,description:txtDescription},function(data)
					{
						if(data==1)
						{
							alert("The Page was successfully updated. Thank You");
							window.location.reload();
						}
						else
						{
							alert("Problem Occured while deactivating this Page. Please try again later.");
							return false;
						}
					},"text");
		}
		else
		{
			alert("Text descrption can not be blank");
			return false;
		}
	}
	else
	{
		alert("The server is too busy. Please try again later.");
	}
}

function sendContactUs()
{
	var fname = $("#txtFirstName").val();
	var lname = $("#txtLastName").val();
	var phone = $("#txtPhoneNumber").val();
	var email = $("#txtEmailAddress").val();
	var comments = $("#txtAreaComments").val();
	
	var strErrMsg = "";
	
	if(fname.length==0)
	{
		strErrMsg += "Please enter your first name.";
		strErrMsg +="\n";
	}
	if(phone.length==0)
	{
		if(email.length==0)
		{
			strErrMsg +="Please enter either email address or phone number.";
			strErrMsg +="\n";
		}
	}
	if(email.length==0)
	{
		if(phone.length==0)
		{
			strErrMsg +="Please enter either email address or phone number.";
			strErrMsg +="\n";
		}
	}
	if(comments.length==0)
	{
		strErrMsg +="Please enter your comment.";
		strErrMsg +"\n";
	}
	
	if(strErrMsg.length>0)
	{
		alert(strErrMsg);
		return false;
	}
	else
	{
		$.post("save-contact-us.php",{action:"save",fname:fname,lname:lname,phone:phone,email:email,comments:comments},function(data){
			
			if(data==1)
			{
				alert("Your Request has been sent to the our representative. We will contact you soon. Thank you for contacting us.");
			}
			else
			{
				alert("We are unable to send your request at this moment. Please try again later.");
			}
			
		},"text");
	}
	
}

function ContinueWith(id)
{
	$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$("#li"+id).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
	$("#tab"+id).fadeIn(); //Fade in the active content
		return false;
}

function SaveData()
{
	alert("Data Saved Successfully");
	return false;
}

function saveTestDrive()
{
	var firstName = document.getElementById("txtFirstName").value;
	var lastName = document.getElementById("txtLastName").value;
	var phone = document.getElementById("txtPhoneNumber").value;
	var email = document.getElementById("txtEmailAddress").value;
	var time = document.getElementById("timepicker").value;
	var date = document.getElementById("datepicker").value;
	//var car_model = document.getElementById("ddlcar_model").value;
	var choiceofday = document.getElementById("txtDay").value;
	
	
	var strErrMsg = "";
	
	if(firstName.length==0)
	{
		strErrMsg +"\n";
		strErrMsg += "Please enter your first name.";
		strErrMsg +="\n";
	}
	if(phone.length==0)
	{
		if(email.length==0)
		{
			strErrMsg +"\n";
			strErrMsg +="Please enter either email address or phone number.";
			strErrMsg +="\n";
		}
	}
	if(email.length==0)
	{
		if(phone.length==0)
		{
			strErrMsg +"\n";
			strErrMsg +="Please enter either email address or phone number.";
			strErrMsg +="\n";
		}
	}
	if(date.length==0)
	{
		strErrMsg +"\n";
		strErrMsg +="Please enter date of your schedule.";
		strErrMsg +"\n";
	}
	if(time.length==0)
	{
		strErrMsg +"\n";
		strErrMsg +="Please enter time of your schedule.";
		strErrMsg +"\n";
	}
	
	if(strErrMsg.length>0)
	{
		alert(strErrMsg);
		return false;
	}
	else
	{
		$.post("common-save-functions.php",{action:"save-test-drive",fname:firstName,lname:lastName,phone:phone,email:email,time:time,date:date,choiceofday:choiceofday},function(data){
			if(data==1)
			{
				alert("Thank you for choosing the test drive. We will certainly call you soon.");
			}
			else
			{
				alert("We are unable to take your booking at this moment. Please try again later.");
			}
			
		},"text");
	}
}

function sendFriendEmail(URL)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=690,height=500,left = 295,top = 100');");
}

function showVin()
{
	
	if(document.getElementById('chkVinXml').checked==true)
	{
		//alert(document.getElementById('chkVinXml').checked);
		document.getElementById('vin1').style.display="block";
		document.getElementById('vin2').style.display="block";
	}
	else
	{
		document.getElementById('vin1').style.display="none";
		document.getElementById('vin2').style.display="none";
	}
}

function showVehicleDetails(uid,pagename)
{
	document.getElementById('hdnSelectedVehicleUid').value=uid;
	var URL = "vehicle-details";
	var frmName="";
	if(pagename=="vehicle")
	{
		document.frmVehiclePage.action=URL;
		document.frmVehiclePage.submit();
	}
	else if(pagename=="index")
	{
		document.frmIndex.action=URL;
		document.frmIndex.submit();
	}
	
}

function gotoImageUpload()
{
	if(confirm("Clicking on 'ok' you will be moved to the image upload page.Are you sure you want to leave the form entry page?"))
	{
		var URL = "upload-vehicle-images.php";
		window.open(URL,"_self");
	}
}

function newsearch()
{

	day = new Date();
	var randtime = day.getTime();
	var selectedType = document.getElementById('vehicletype').value;
	var selectedModel = document.getElementById('vehiclemodel').value;
	var selectedMake = document.getElementById('vehiclemake').value;
	var selectedYear = document.getElementById('vehicleyear').value;
	var selectedKM = document.getElementById('vehiclekm').value;
	var selectedPrice = document.getElementById('vehicleprice').value;
	
	//document.getElementById('hdnSelectedType').value=selectedType;
	//document.getElementById('hdnSelectedMake').value=selectedMake;
	//document.getElementById('hdnSelectedModel').value=selectedModel;
	//document.getElementById('hdnSelectedYear').value=selectedYear;
	//document.getElementById('hdnSelectedKm').value=selectedKM;
	//document.getElementById('hdnSelectedPrice').value=selectedPrice;
	
	var URL = "vehicles?action=new-search&randompass="+randtime;
	document.frmVehiclePage.action=URL;
	document.frmVehiclePage.submit();
}

function sendMoreInfo()
{
	var firstName = document.getElementById('txtFirstName').value;
	var lastName = document.getElementById('txtLastName').value;
	var phoneNumber = document.getElementById('txtPhoneNumber').value;
	var emailId = document.getElementById('txtEmailAddress').value;
	var comments = document.getElementById('txtComments').value;
	var selectedVin = document.getElementById('hdnSelectedVin').value;
	
	if(selectedVin.length>0)
	{
		if(firstName.length==0 || emailId.length==0)
		{
			if(firstName.length==0)
			{
				alert("Please enter your first name. Thank you");
				return false;
			}
			if(emailId.length==0)
			{
				alert("Please enter your email address. Thank you");
				return false;
			}
		}
		else
		{
			$.post("sendMoreInfo.php",{selectedVin:selectedVin,fname:firstName,lname:lastName,phone:phoneNumber,emailId:emailId,comments:comments},function(data){
				if(data==1)
				{
					alert("Thank you for showing interest in our vehicles. We will contact you soon. Thank You.");
				}
			},"text");
		}
	}
	else
	{
		alert("There was a error while selecting the VIN#. Please visit the vehicles page first. Thank you.");
		return false;
	}
}

function EditContent(id)
{
	alert("This functionlaity is under construction. Thank you");
	return false;
}

function DeleteContent(id)
{
	alert("This functionlaity is under construction. Thank you");
	return false;
}

//===================================FEATURED VEHICLE==============================================
function makeFeatured(id,domain)
{
	if(id.length>0)
	{
		$.post("vehicle-add-edit-delete-featured.php",{action:"featured",id:id,domain:domain},function(data){
			if(data==1)
			{
				alert("This vehicle has been made as featured. Thank you");
			}
			else
			{
				alert("There was a error while making it featured. Please try later. Thank you");
			}
			
		},"text");
	}
}

function RemoveFeatured(id)
{
	if(id.length>0)
	{
		$.post("vehicle-add-edit-delete-featured.php",{action:"remove-featured",id:id,domain:domain},function(data){
			if(data==1)
			{
				alert("This vehicle has been made removed from featured. Thank you");
				window.location.reload();
			}
			else
			{
				alert("There was a error while removing from featured. Please try later. Thank you");
			}
			
		},"text");
	}
}
//========================================ENDS===========================================================

